package com.allinpay.merchantweb.service.impl;

import java.math.BigDecimal;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.allinpay.merchantweb.common.utils.SequenceUtil;
import com.allinpay.merchantweb.constants.ReturnCodeConstants;
import com.allinpay.merchantweb.model.EnterpriseProdAduitInfo;
import com.allinpay.merchantweb.model.EnterpriseProdInfo;
import com.allinpay.merchantweb.model.EnterpriseProdInfoVo;
import com.allinpay.merchantweb.model.PageForLayui;
import com.allinpay.merchantweb.request.CommonReqBean;
import com.allinpay.merchantweb.response.AppCommonResponse;
import com.allinpay.merchantweb.response.ResponseData;
import com.allinpay.merchantweb.service.IProductionService;
import com.google.gson.reflect.TypeToken;

@Service
public class ProductionServiceImpl implements IProductionService{

	@Autowired
	RestRequestService restRequestService;
	
	
	@Override
	public ResponseData<EnterpriseProdInfoVo> queryEnterpriseProds(EnterpriseProdInfo prodInfo) throws Exception {
		// TODO Auto-generated method stub
		CommonReqBean reqBean = new CommonReqBean(); 
		reqBean.setBizContent(prodInfo);
		reqBean.setReqSsn(SequenceUtil.getAppReqSsn());
		
		
		AppCommonResponse<ResponseData<EnterpriseProdInfoVo>> respBean = restRequestService.postToAppForSingleByObj
				("/production/queryEnterpriseProdList"
						, reqBean
						, new TypeToken<AppCommonResponse<ResponseData<EnterpriseProdInfoVo>>>() {}.getType());
		if( respBean != null &&  respBean.getSubCode() != null 
				&& respBean.getSubCode().equals(ReturnCodeConstants.SUB_CODE_SUCC)) {			
			List<EnterpriseProdInfoVo> prodList = respBean.getData().getDataList();
			for( EnterpriseProdInfoVo prodInfoTp : prodList ) {
				prodInfoTp.translateSomeItems();
			}
			return respBean.getData();
		}else {
			throw new Exception("queryEnterpriseProdList error:" + respBean.getSubMsg());
		}
	}

	@Override
	public Object addEnterpriseProd(EnterpriseProdInfo prodInfo) throws Exception {
		// TODO Auto-generated method stub
		CommonReqBean reqBean = new CommonReqBean(); 
		reqBean.setBizContent(prodInfo);
		reqBean.setReqSsn(SequenceUtil.getAppReqSsn());
		
		
		AppCommonResponse<ResponseData> respBean = restRequestService.postToAppForSingleByObj
				("/production/addEnterpriseProd"
						, reqBean
						, new TypeToken<AppCommonResponse<ResponseData>>() {}.getType());
		if( respBean != null &&  respBean.getSubCode() != null 
				&& respBean.getSubCode().equals(ReturnCodeConstants.SUB_CODE_SUCC)) {			
			return respBean;
		}else {
			throw new Exception("addEnterpriseProd error:" + respBean);
		}
	}

	@Override
	public Object updateEnterpriseProd(EnterpriseProdInfo prodInfo, Integer instanceId, String accountCode) throws Exception {
		// TODO Auto-generated method stub
		
//		Integer instanceId = enterpriseProdInfo.getInstanceId();
//		enterpriseProdAduitInfo.setOperDate(sdf.parse(operDate));
//		enterpriseProdAduitInfo.setAuditStatus("1");
//		enterpriseProdAduitInfo.setOldRateValue(enterpriseProdInfo.getProdRate());
//		enterpriseProdAduitInfo.setOldLimitValue(enterpriseProdInfo.getProdRaiseLimit());
//		enterpriseProdAduitInfo.setOldUpmoneyValue(enterpriseProdInfo.getProdBuyMinmonye());
//		companyService.submitProdUpData(enterpriseProdAduitInfo);
		
		EnterpriseProdAduitInfo enterpriseProdAduitInfo=new EnterpriseProdAduitInfo();
		enterpriseProdAduitInfo.setProdId(prodInfo.getProdId());
		enterpriseProdAduitInfo.setInstanceId(instanceId);
		enterpriseProdAduitInfo.setOperAccountCode(accountCode);
		enterpriseProdAduitInfo.setNewRateValue(prodInfo.getProdRate());
		enterpriseProdAduitInfo.setNewLimitValue(prodInfo.getProdRaiseLimit());
		enterpriseProdAduitInfo.setNewUpmoneyValue(prodInfo.getProdBuyMinmonye());
		enterpriseProdAduitInfo.setNewAlterRate(prodInfo.getProdAlterRate());

		
		
		CommonReqBean reqBean = new CommonReqBean(); 
		reqBean.setBizContent(enterpriseProdAduitInfo);
		reqBean.setReqSsn(SequenceUtil.getAppReqSsn());		
		
		
		AppCommonResponse<ResponseData> respBean = restRequestService.postToAppForSingleByObj
				("/production/updateEnterpriseProd"
						, reqBean
						, new TypeToken<AppCommonResponse<ResponseData>>() {}.getType());
		if( respBean != null &&  respBean.getSubCode() != null 
				&& respBean.getSubCode().equals(ReturnCodeConstants.SUB_CODE_SUCC)) {			
			return respBean;
		}else {
			throw new Exception("updateEnterpriseProd error:" + respBean);
		}
	}

	@Override
	public Object deleteEnterpriseProd(EnterpriseProdInfo prodInfo) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object auditEnterpriseProd(int prodAuditId, Integer instanceId, String accountCode)
			throws Exception {
		// TODO Auto-generated method stub
		EnterpriseProdAduitInfo enterpriseProdAuditInfo = new EnterpriseProdAduitInfo();
		enterpriseProdAuditInfo.setId(prodAuditId);
		enterpriseProdAuditInfo.setInstanceId(instanceId);
		enterpriseProdAuditInfo.setAuditAccountCode(accountCode);
		
		CommonReqBean reqBean = new CommonReqBean(); 
		reqBean.setBizContent(enterpriseProdAuditInfo);
		reqBean.setReqSsn(SequenceUtil.getAppReqSsn());		
		
		
		AppCommonResponse<ResponseData> respBean = restRequestService.postToAppForSingleByObj
				("/production/auditEnterpriseProd"
						, reqBean
						, new TypeToken<AppCommonResponse<ResponseData>>() {}.getType());
		if( respBean != null &&  respBean.getSubCode() != null 
				&& respBean.getSubCode().equals(ReturnCodeConstants.SUB_CODE_SUCC)) {			
			return respBean;
		}else {
			throw new Exception("auditEnterpriseProd error:" + respBean);
		}
		
	}

	@Override
	public ResponseData<EnterpriseProdAduitInfo> queryEnterpriseProdAudits(EnterpriseProdAduitInfo aduitInfo) throws Exception {
		// TODO Auto-generated method stub
		CommonReqBean reqBean = new CommonReqBean(); 
		reqBean.setBizContent(aduitInfo);
		reqBean.setReqSsn(SequenceUtil.getAppReqSsn());
		
		
		AppCommonResponse<ResponseData<EnterpriseProdAduitInfo>> respBean = restRequestService.postToAppForSingleByObj
				("/production/queryEnterpriseProdAuditList"
						, reqBean
						, new TypeToken<AppCommonResponse<ResponseData<EnterpriseProdAduitInfo>>>() {}.getType());
		if( respBean != null &&  respBean.getSubCode() != null 
				&& respBean.getSubCode().equals(ReturnCodeConstants.SUB_CODE_SUCC)) {			
//			List<EnterpriseProdInfoVo> prodList = respBean.getData().getDataList();
//			for( EnterpriseProdInfoVo prodInfoTp : prodList ) {
//				prodInfoTp.translateSomeItems();
//			}
			return respBean.getData();
		}else {
			throw new Exception("queryEnterpriseProdList error:" + respBean.getSubMsg());
		}
	}


}
